package com.happy.Animation.controllers;

import com.happy.Animation.dao.AuthorRepository;
import com.happy.Animation.entities.Author;
import org.apache.catalina.LifecycleState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("authors")
public class AuthorController {
    @Autowired
    AuthorRepository authorRepository;

    @GetMapping
    public String displayAuthors(Model model){
        List<Author> authors =authorRepository.findAll();
        model.addAttribute("authors",authors);
        return "authors/list-authors";
    }

    @GetMapping("/new")
    public String displayAuthorForm(Model model){
        Author aAuthor = new Author();
        model.addAttribute("author",aAuthor);
        return "authors/new-author";
    }
    @PostMapping("/save")
    public String createAuthor(Model model,Author author){
        authorRepository.save(author);

        return "redirect:/authors/new";
    }
}
